/**
 * 
 */
package co.gov.medellin.sdep.sisdep.gestion.service;

import java.util.List;

import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.gestion.exception.VehiculoException;
import co.gov.medellin.sdep.sisdep.gestion.exception.VehiculoRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.exception.descriptor.VehiculoMessageDescriptor;
import co.gov.medellin.sdep.sisdep.model.Vehiculo;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IVehiculoDAOT;

/**
 * @author acespedes
 *
 */
public class VehiculoServiceImpl extends BaseService implements IVehiculoService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IVehiculoDAOT vehiculoDAO;

	/**
	 * 
	 */
	public VehiculoServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Vehiculo vehiculo) throws VehiculoException {
		try{
			vehiculo.setUsuarioCrea(getPrincipalUser());
			//vehiculo.setFechaCrea(new Date());
			getVehiculoDAO().insert(vehiculo);
		}catch (Exception e) {
			Object [] infoError = {
				vehiculo.getPlaca()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new VehiculoException(new VehiculoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new VehiculoException(new VehiculoMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Vehiculo vehiculo) throws VehiculoException {
		try{
			vehiculo.setUsuarioModifica(getPrincipalUser());
			//vehiculo.setFechaModifica(new Date());
			getVehiculoDAO().update(vehiculo);
		}catch (Exception e) {
			Object [] infoError = {
				vehiculo.getPlaca()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new VehiculoRuntimeException(new VehiculoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new VehiculoRuntimeException(new VehiculoMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Vehiculo vehiculo) throws VehiculoException {
		try {
			vehiculo.setUsuarioModifica(getPrincipalUser());
			//vehiculo.setFechaModifica(new Date());
			getVehiculoDAO().delete(vehiculo);
		}catch (Exception e) {
			Object [] infoError = {
				vehiculo.getPlaca()
			};
			throw new VehiculoRuntimeException(new VehiculoMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Vehiculo vehiculo) throws VehiculoException {
		try{
			//return getVehiculoDAO().getAllVehiculoPaginated(start, max, criteria);
			return getVehiculoDAO().load(vehiculo);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new VehiculoRuntimeException(new VehiculoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Vehiculo get(long id) throws VehiculoException {
		try{
			return getVehiculoDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new VehiculoRuntimeException(new VehiculoMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	public Vehiculo findByRelated(String placa) {
		try{
			Vehiculo criteria = new Vehiculo();
			criteria.setPlaca(placa);
			return getVehiculoDAO().findByRelated(criteria);
		}catch (Exception e) {
			Object [] infoError = {placa};
			throw new VehiculoRuntimeException(new VehiculoMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IVehiculoDAOT getVehiculoDAO() {
		return vehiculoDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setVehiculoDAO(IVehiculoDAOT vehiculoDAO) {
		this.vehiculoDAO = vehiculoDAO;
	}
	
	//touch comment

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
